//
//  Text.m
//  2D绘图（文字、图片）
//
//  Created by apple on 15/9/18.
//  Copyright (c) 2015年 戴维营. All rights reserved.
//

#import "Text.h"

@implementation Text

{
    BOOL  _clockWise;
}


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.


- (void)drawRect:(CGRect)rect
{
#if 0
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextAddRect(context, CGRectMake(50, 50, 100, 100));
    
     CGContextClip(context);
    
//    [[UIColor redColor] setFill];
    
//    CGContextFillPath(context);
    
   
//    [[UIColor greenColor] setFill];
    
    CGContextAddEllipseInRect(context, CGRectMake(150, 150, 50, 50));
    
    //非零环绕数；
    
//    CGContextClip(context);
    
//    CGContextFillPath(context);
    
    
    //创建颜色空间
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    CGFloat components[] = {
        1.0, 1.0, 1.0, 0.5,
        0.0, 1.0, 0.0, 1.0,
//        0.0, 0.0, 1.0, 1.0
    };
    CGFloat locations[] = {0.0,1.0};
    
    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, 2);
    
    
    CGPoint startCenter = CGPointMake(50, 50);
    CGPoint endCenter = CGPointMake(100, 100);
    CGContextDrawRadialGradient(context, gradient, startCenter, 0, endCenter, 100, 0);
    
    CGGradientRelease(gradient);
    //释放
    CGColorSpaceRelease(colorSpace);


#endif
    
#if 0
    
    NSString *str = @"天王盖地虎";
    
    NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor greenColor],NSBackgroundColorAttributeName:[UIColor redColor]};
    
    //从（100，100）开始绘制
    
//    [str drawAtPoint:CGPointMake(100, 100) withAttributes:dict];
    
    //文字绘制在一个矩形中
    
    [str drawInRect:CGRectMake(50,50 , 50, 100) withAttributes:dict];
#endif
    
    
#if 0
    
    UIImage *image = [UIImage imageNamed:@"123"];
    
    //按原始大小绘制
    
//    [image drawAtPoint:CGPointZero];
    
    //拉伸绘制在矩形中
    
//    [image drawInRect:CGRectMake(100, 100, 100, 100)];
    
    //在矩形中平铺
    
    [image drawAsPatternInRect:self.bounds];
    
#endif
    
#if 0
//    线性渐变
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    //创建颜色空间
    
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    
    CGFloat components[] =
    {
        1.0,0.0,0.0,1.0,//red;
        
        0.0,1.0,0.0,1.0,//green;
        
        0.0,0.0,1.0,1.0 //blue;
        
    };
    
    CGFloat locations[] = {0,0.5,1.0};
    
    //创建渐变
    
    CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, components, locations, 3);
    
//    //绘制线性渐变
    
    CGPoint startPoint = CGPointMake(100, 100);
    
    CGPoint endPoint = CGPointMake(100, 200);
    
    CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
    
    //C语言的，所以需要手动释放
    
    CGGradientRelease(gradient);
    
    CGColorSpaceRelease(colorSpace);
    
    
    
//    
////    //绘制辐射渐变
//    
//    CGPoint startCenter = CGPointMake(200, 200);
//    
//    CGPoint endCenter = CGPointMake(300, 300);
//    
//    CGContextDrawRadialGradient(context, gradient, startCenter, 0, endCenter, 50, 0);
//    

#endif
    
#if 0
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextAddRect(context, CGRectMake(100, 100, 100, 100));
    
    [[UIColor redColor] setFill];
    
    CGContextFillPath(context);
   
    
    CGContextSaveGState(context);
    
    CGContextAddEllipseInRect(context, CGRectMake(0, 0, 50, 50));
    
    [[UIColor greenColor] setFill];
    
    CGContextRestoreGState(context);
    
    CGContextFillPath(context);
    
    
#endif
#if 1

    CGContextRef context = UIGraphicsGetCurrentContext();


#if 0
    static CGFloat i = 0.0f;
    
    CGContextAddArc(context, 200, 200, 100, 0, M_PI * i, 0);
    
    CGContextAddArc(context, 200, 200, 10, M_PI * i, 0, 1);
    
//    
    if (_clockWise)
    {
        i -= 0.01;
        if (i < 0)
        {
            i = 0.0;
            
            _clockWise = NO;
        }
    }
    else
    {
        i += 0.01;
        if (i > 2.0)
        {
            _clockWise = YES;
        }
    }
#endif
    
    CGContextAddRect(context, CGRectMake(150, 150, 150, 150));
    
    [[UIColor greenColor] setFill];
    
//    CGContextDrawPath(context, kCGPathEOFillStroke);
    
//    CGContextStrokePath(context);
//    CGContextDrawPath(context, kCGPathEOFillStroke);
    
//    CGContextClip(context);
   
#if 0
//    CGContextEOClip(context);
    
    UIImage *image = [UIImage imageNamed:@"234.png"];
    
    [image drawInRect:CGRectMake(100, 100, 200, 200)];
    
#endif
#if 1
    
    CGContextAddRect(context, CGRectMake(100, 100, 200, 200));
    
    [[UIColor redColor] setFill];

    CGContextDrawPath(context, kCGPathEOFillStroke);
    
#endif
    
#endif
    
}


@end
